#include "list.h"

list_p list_add( list_pp f, data_p d )
{
    list_p ret = (list_p) calloc( 1, sizeof(list_t) );
    list_p first = *f;
    ret->data = d;

    if( first )
        first->previous = ret;
    ret->next = first;
    *f = ret;
    return ret;
}

int list_rem( list_pp f, list_p l )
{
    if( !f )
        return 0;
    if( !*f )
        return 0;
    if( !list_find( *f, l->data ) )
        return 0;

    if( l->next )
        l->next->previous = l->previous;

    if( l->previous )
        l->previous->next = l->next;
    else
        *f = l->next;

    free( l );
    return 1;
}

list_p list_find( list_p f, data_p d )
{
    list_p list;

    for( list = f ; list ; list = list->next )
        if( list->data == d )
            return list;

    return 0;
}
